package cn.keniykey.mc.landterritory.land;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.config.WorldsConfiguration;
import cn.keniykey.mc.landterritory.event.PlayerEnterLeaveLandEvent;
import cn.keniykey.mc.landterritory.land.api.Land;
import cn.keniykey.mc.landterritory.toolkit.Util;
import net.md_5.bungee.api.ChatMessageType;

public final class LandEnterLeaveCheck implements Runnable{
	
	private static final String[] LANDTIP_REPLACE_KEY = new String[] {"<player>","<id>","<name>","<owner>","<size>","<world>"};
	public enum EEnterLeaveTipLocation {
		CHAT,INVENTORY;
	}

	@Override
	public void run() {
		for(final Player player : Bukkit.getOnlinePlayers()) {
			final Location player_loc = player.getLocation();
			final Land oldland = LandManager.getLandManager().getCircleDetectionLand(player);
			final Land nowland = LandManager.getLandManager().getHighestPriorityLand(player_loc);
			if(oldland == null && nowland == null) continue;
			if(oldland != null && nowland != null && oldland.equals(nowland)) continue;
			if(nowland == null) {
				LandManager.getLandManager().conmap_players_in.remove(player.getUniqueId());
			}else {
				LandManager.getLandManager().conmap_players_in.put(player.getUniqueId(), nowland);
			}
			final PlayerEnterLeaveLandEvent event = new PlayerEnterLeaveLandEvent(player, nowland, oldland);
			sendEnterLeaveMessage(event);
			Bukkit.getServer().getPluginManager().callEvent(event);
		}
	}
	private static void sendEnterLeaveMessage(final PlayerEnterLeaveLandEvent e) {
		final Player player = e.getPlayer();
		final Land land_enter = e.getEnterLand();
		final Land land_leave = e.getLeaveLand();
		if(land_leave != null && land_leave.getLeaveMessage() != null) {
			final String str_leave = LanguageManager.getLang(1002000) + Util.conver(coverString(land_leave.getLeaveMessage(),
					player, land_leave));
			switch(ConfigurationManager.View_LandInfo_Tip_Loc) {
			case INVENTORY:
				if(land_enter == null || land_enter.getEnterMessage() == null) {
					final net.md_5.bungee.api.chat.TextComponent text_leave = new net.md_5.bungee.api.chat.TextComponent(str_leave);
					player.spigot().sendMessage(ChatMessageType.ACTION_BAR, text_leave);
				}
				break;
			default:
				player.sendMessage(str_leave);
				break;
			}
		}
		if(land_enter != null && land_enter.getEnterMessage() != null) {
			final String str_enter = LanguageManager.getLang(1002001) + Util.conver(coverString(land_enter.getEnterMessage(),
					player, land_enter));
			switch(ConfigurationManager.View_LandInfo_Tip_Loc) {
			case INVENTORY:
				final net.md_5.bungee.api.chat.TextComponent text_enter = new net.md_5.bungee.api.chat.TextComponent(str_enter);
				player.spigot().sendMessage(ChatMessageType.ACTION_BAR, text_enter);
				break;
			default:
				player.sendMessage(str_enter);
				break;
			}
		}
	}
	
	private static String coverString(String str,Player player,Land land) {
		OfflinePlayer offplayer = Bukkit.getPlayer(land.getOwner());
		if(offplayer == null) offplayer = Bukkit.getOfflinePlayer(land.getOwner());
		return coverString(str, LANDTIP_REPLACE_KEY, player.getName(),land.getID(),land.getName(),
				offplayer.getName(),land.getRangeSize(), WorldsConfiguration.getWorldName(land.getWorldName()));
	}
	private static String coverString(final String oldmes,final String[] olds,final Object...news) {
		String newmes = oldmes;
		for(int a = 0; a < olds.length && a < news.length; a++) {
			newmes = newmes.replaceAll(olds[a], String.valueOf(news[a]));
		}
		return newmes;
	}
}